//
// RS Game Framework
// Copyright © 2010 Jedd Haberstro
// jhaberstro@gmail.com
// 
// $Id:
//

#ifndef RS_GRAPHICS_VERTEXFORMAT_HPP
#define RS_GRAPHICS_VERTEXFORMAT_HPP

#include "rs/graphics/GraphicsForwards.hpp"
#include "rs/reflection/Reflection.hpp"
#include "rs/ReferenceCounted.hpp"
#include <vector>

namespace rs
{
    namespace graphics
    {
        class VertexFormat : public ReferenceCounted
        {
            friend class Renderer;
            friend class GLRenderer;
            
            RS_DECLARE_REFLECTION();
            
        protected:
            typedef std::vector< InputElement > InputElementContainer;
              
        public:
                        
            InputElement const& GetInputElement(UInt32 i) const;
            
            RsSize GetInputElementCount() const;
            
            UInt32 GetVertexSize() const;
                        
        protected:
            
            VertexFormat(InputElement const* inputElements, RsSize count);
          
        protected:
                      
            InputElementContainer inputElements_;
            UInt32 size_;
        };
        
        
        inline VertexFormat::VertexFormat(InputElement const* inputElements, RsSize count)
        : inputElements_(inputElements, inputElements + count),
		  size_(0) {
            RS_ASSERT(inputElements != 0, "inputElements is null");
            for (RsSize i = 0; i < count; ++i) {
                size_ += GetInputElementFormatSize(inputElements[i].format) * inputElements[i].componentCount;
            }
        }
                
        inline InputElement const& VertexFormat::GetInputElement(UInt32 i) const {
            RS_ASSERT(i < inputElements_.size(), "index 'i' is out of range");
            return inputElements_[i];
        }
        
        inline RsSize VertexFormat::GetInputElementCount() const {
            return inputElements_.size();
        }
        
        inline UInt32 VertexFormat::GetVertexSize() const {
            return size_;
        }
        
    }
}

#endif // RS_GRAPHICS_VERTEXFORMAT_HPP